﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

namespace E.Story
{
    /// <summary>
    /// 故事UI（可根据实际项目需求修改此脚本）
    /// </summary>
    public class StoryUI : MonoBehaviour
    {
        [Header("主要组件")]
        [SerializeField] private StoryRunner storyRunner;

        [Header("UI组件")]
        /* UI画布 */
        [SerializeField] private Canvas cvsStoryUI;

        /* 菜单 */
        [SerializeField] private Button btnStart;

        /* 背景 */
        [SerializeField] private Image imgBGI;

        /* 立绘面板 */
        [SerializeField] private GameObject panPortraits;
        [SerializeField] private Image imgPortrait_00;
        //[SerializeField] private Image imgPortrait_01;
        //[SerializeField] private Image imgPortrait_02;

        /* 对话面板 */
        [SerializeField] private GameObject panDialogue;
        [SerializeField] private GameObject panRoleName;
        [SerializeField] private Text txtRoleName;
        [SerializeField] private Text txtContent;
        [SerializeField] private Button btnContinue;

        /* 选项面板 */
        [SerializeField] private GameObject panBranchs;
        [SerializeField] private UIBranchList uiBranchList;

        /* 变量面板 */
        [SerializeField] private GameObject panVars;
        [SerializeField] private UIVarList uiVarList;

        private void Start()
        { 
            // 绑定点击事件
            btnStart.onClick.AddListener(() => storyRunner.StartStory());
            btnContinue.onClick.AddListener(() => storyRunner.NextStep());
        }

        /// <summary>
        /// 显示菜单
        /// </summary>
        public void ShowMenu()
        {
            btnStart.gameObject.SetActive(true);
        }

        /// <summary>
        /// 隐藏菜单
        /// </summary>
        public void HideMenu()
        {
            btnStart.gameObject.SetActive(false);
        }

        /// <summary>
        /// 显示背景
        /// </summary>
        /// <param name="sprite">背景</param>
        public void ShowBGI(Sprite sprite)
        {
            imgBGI.gameObject.SetActive(true);
            imgBGI.sprite = sprite;
        }

        /// <summary>
        /// 隐藏背景
        /// </summary>
        public void HideBGI()
        {
            imgBGI.gameObject.SetActive(false);
        }

        /// <summary>
        /// 显示立绘
        /// </summary>
        /// <param name="sprite">立绘</param>
        public void ShowPortrait(Sprite sprite, int index = 0)
        {
            panPortraits.SetActive(true);
            imgPortrait_00.sprite = sprite;
            imgPortrait_00.enabled = true;
        }

        /// <summary>
        /// 隐藏立绘
        /// </summary>
        public void HidePortrait(int index = 0)
        {
            panPortraits.SetActive(false);
            imgPortrait_00.enabled = false;
        }

        /// <summary>
        /// 显示对话面板
        /// </summary>
        /// <param name="rolename">角色名称</param>
        /// <param name="sentenceData">对话内容</param>
        public void ShowDialogue(string rolename, SentenceData sentenceData)
        {
            panDialogue.SetActive(true);
            panRoleName.SetActive(!string.IsNullOrEmpty(rolename));

            txtRoleName.text = rolename;
            txtContent.text = sentenceData.Text;
        }

        /// <summary>
        /// 隐藏对话面板
        /// </summary>
        public void HideDialogue()
        {
            panDialogue.SetActive(false);
        }

        /// <summary>
        /// 显示选项面板
        /// </summary>
        /// <param name="choiceDatas">选项数据</param>
        public void ShowBranchs(List<ChoiceData> choiceDatas)
        {
            panBranchs.SetActive(true);
            uiBranchList.SetDatas(choiceDatas);
        }

        /// <summary>
        /// 隐藏选项面板
        /// </summary>
        public void HideBranchs()
        {
            panBranchs.SetActive(false);
        }

        /// <summary>
        /// 显示变量面板
        /// </summary>
        /// <param name="varDatas">变量数据列表</param>
        public void ShowVars(List<VarData> varDatas)
        {
            panVars.SetActive(true);
            uiVarList.SetDatas(varDatas);
        }

        /// <summary>
        /// 隐藏变量面板
        /// </summary>
        public void HideVars()
        {
            panVars.SetActive(false);
        }

        /// <summary>
        /// 刷新变量面板
        /// </summary>
        public void UpdateVars()
        {
            uiVarList.Refresh();
        }
    }
}